package com.example.shopping_li.cart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shopping_li.R;
import com.example.shopping_li.URLUtils;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

public class CartAdapter extends BaseAdapter {
Context context;
    List<CartBean.DataBean> list;
    List<Boolean>statuList;//是否选中
    public CartAdapter(Context context, List<CartBean.DataBean> list, List<Boolean>statuList) {
        this.context = context;
        this.list = list;
        this.statuList=statuList;
    }
    @Override
    public int getCount() { return list.size(); }
    @Override
    public Object getItem(int position) { return list.get(position); }
    @Override
    public long getItemId(int position) { return position; }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_cart,null);
            vh = new VH(convertView);
            convertView.setTag(vh);
        }else{
            vh = (VH) convertView.getTag();
        }
        CartBean.DataBean dataBean = list.get(position);
        String picurl = URLUtils.PUBLIC_URL+dataBean.getCommodityPhoto();
        Picasso.with(context).load(picurl).into(vh.iv);
        vh.titleTv.setText(dataBean.getCommodityName());
        vh.infoTv.setText(dataBean.getCommodityInfo());
        vh.priceTv.setText(dataBean.getUnivalence()+"");
        vh.numTv.setText("x"+dataBean.getCount()+"");
//
        boolean sb=statuList.get(position);
vh.cb.setChecked(sb);
//设定监听事件
        vh.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                statuList.set(position,!sb);
                if (selectGoodsListener != null) {
                    selectGoodsListener.selectGoods(!sb,position);
                }
            }
        });
        return convertView;

    }
    public interface SelectGoodsListener{
        public void selectGoods(boolean status,int pos);
    }
    SelectGoodsListener selectGoodsListener;

    public void setSelectGoodsListener(SelectGoodsListener selectGoodsListener) {
        this.selectGoodsListener = selectGoodsListener;
    }

    class VH{
        ImageView iv;
        TextView titleTv,infoTv,priceTv,numTv;
        CheckBox cb;
        public VH(View v){
            iv = v.findViewById(R.id.item_cart_iv);
            titleTv=v.findViewById(R.id.item_cart_tv_title);
            infoTv = v.findViewById(R.id.item_cart_tv_info);
            priceTv=v.findViewById(R.id.item_cart_tv_price);
            numTv = v.findViewById(R.id.item_cart_tv_num);
            cb=v.findViewById(R.id.item_cart_cb);

        }
    }
}